import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class ExecuteSqlTraitService extends BaseService<any> {
  private readonly logger = new Logger(ExecuteSqlTraitService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getSqlQuery
   * 对应 PHP: ExecuteSqlTrait_admin::getSqlQuery()
   * 逻辑类型: undefined - undefined
   */
  async getSqlQuery(sql_data: any) {
    // 基于PHP真实逻辑: getSqlQuery
    // PHP原文: $sql_data = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql_data);          $sql_data ...
const sql_data = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; )+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", sql_data];

        const sql_data = str_replace("\r", "\n", sql_data];
        const sql_query = [);
        const num = 0;
        const sql_arr = explode(";\n", trim(sql_data)];
        unset(sql);
        foreach (sql_arr as sql) {
            sql_query[ num ] = '';
            const sqls = explode("\n", trim(sql)];
            const sqls = array_filter(sqls);
            foreach (sqls as query) {
                const str1 = query[0] || '';
                if (str1 != '#' && str1 != '-')
                    sql_query[ num ]  +=  query;
            }
            num++;
  }

  /**
   * handleSqlPrefix
   * 对应 PHP: ExecuteSqlTrait_admin::handleSqlPrefix()
   * 逻辑类型: undefined - undefined
   */
  async handleSqlPrefix(sql: any, prefix: any) {
    // 基于PHP真实逻辑: handleSqlPrefix
    // PHP原文: if (str_contains($sql, 'CREATE TABLE')) {             $match_item = preg_match('/CREATE TABLE [`]?(\\w+)[`]?/is', $sql, $match_data);...
if (str_contains(sql, 'CREATE TABLE')) {
            const match_item = preg_match('/CREATE TABLE [`)?(\\w+)[`]?/is', sql, match_data];
  }
}
